﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;

public class GameJsonUtility
{
    [CompilerGenerated]
    private static System.Func<JToken, bool> <>f__am$cache0;

    public static object FromJson(string json, System.Type type)
    {
        return JsonConvert.DeserializeObject(json, type);
    }

    public static void FromJsonOverwrite(JToken json, object target)
    {
        JsonUtility.FromJsonOverwrite(json.ToString(), target);
        if (<>f__am$cache0 == null)
        {
            <>f__am$cache0 = x => x is JProperty;
        }
        IEnumerator<JProperty> enumerator = json.DeepChildren().Where<JToken>(<>f__am$cache0).Cast<JProperty>().GetEnumerator();
        try
        {
            while (enumerator.MoveNext())
            {
                JProperty current = enumerator.Current;
                string name = current.Name;
                FieldInfo field = target.GetType().GetField(name);
                if (field != null)
                {
                    if (field.FieldType.Is(typeof(JToken)))
                    {
                        field.SetValue(target, current.Value);
                    }
                    else
                    {
                        if ((current.Value.Type == JTokenType.Object) && (field.FieldType == typeof(string)))
                        {
                            field.SetValue(target, current.Value.ToString());
                            continue;
                        }
                        if ((current.Value.Type == JTokenType.String) && (field.FieldType != typeof(string)))
                        {
                            <FromJsonOverwrite>c__AnonStorey68 storey = new <FromJsonOverwrite>c__AnonStorey68 {
                                valueString = current.Value.ToString()
                            };
                            if (field.FieldType.Is(typeof(UnityEngine.Object)))
                            {
                                Asset asset = AssetManager.GetAsset(storey.valueString);
                                object gameObject = null;
                                if (asset != null)
                                {
                                    if (field.FieldType.Is(typeof(GameObject)))
                                    {
                                        gameObject = asset.gameObject;
                                    }
                                    else if (field.FieldType.Is(typeof(Component)))
                                    {
                                        gameObject = (asset.gameObject == null) ? null : asset.gameObject.GetComponent(field.FieldType);
                                    }
                                    else
                                    {
                                        gameObject = asset.objectReference;
                                    }
                                }
                                if (asset == null)
                                {
                                    object[] args = new object[] { storey.valueString, field.FieldType, target };
                                    Debug.LogErrorFormat("Could not find Asset '{0}({1})' (at '{2}')", args);
                                    continue;
                                }
                                field.SetValue(target, gameObject);
                            }
                            if (field.FieldType.Is(typeof(Enum)))
                            {
                                object obj3 = Enum.GetValues(field.FieldType).ToEnumerable().FirstOrDefault<object>(new System.Func<object, bool>(storey.<>m__F5));
                                if (obj3 == null)
                                {
                                    object[] objArray2 = new object[] { field.FieldType, storey.valueString, target };
                                    Debug.LogErrorFormat("Enum '{0}' does not contains value '{1}' (at '{2}')", objArray2);
                                    continue;
                                }
                                field.SetValue(target, obj3);
                            }
                        }
                    }
                }
            }
        }
        finally
        {
            if (enumerator == null)
            {
            }
            enumerator.Dispose();
        }
    }

    public static void FromJsonOverwrite(string json, object target)
    {
        JsonSerializerSettings settings = new JsonSerializerSettings {
            ObjectCreationHandling = ObjectCreationHandling.Replace
        };
        JsonConvert.PopulateObject(json, target, settings);
    }

    public static string ToJson(object obj)
    {
        return ToJson(obj, false);
    }

    private static string ToJson(object obj, bool ignoreDefaultValue)
    {
        if (obj == null)
        {
            return null;
        }
        JsonSerializerSettings settings = new JsonSerializerSettings {
            NullValueHandling = NullValueHandling.Ignore,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            ContractResolver = new ContractResolver()
        };
        if (ignoreDefaultValue)
        {
            settings.DefaultValueHandling = DefaultValueHandling.Ignore;
        }
        return JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
    }

    public static string ToJsonIgnoreDefaultValue(object obj)
    {
        return ToJson(obj, true);
    }

    [CompilerGenerated]
    private sealed class <FromJsonOverwrite>c__AnonStorey68
    {
        internal string valueString;

        internal bool <>m__F5(object x)
        {
            return (x.ToString() == this.valueString);
        }
    }

    public class ContractResolver : DefaultContractResolver
    {
        [CompilerGenerated]
        private static System.Func<MemberInfo, bool> <>f__am$cache0;
        [CompilerGenerated]
        private static System.Func<FieldInfo, bool> <>f__am$cache1;
        [CompilerGenerated]
        private static System.Func<FieldInfo, bool> <>f__am$cache2;
        [CompilerGenerated]
        private static Action<JsonProperty> <>f__am$cache3;

        protected override IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
        {
            <CreateProperties>c__AnonStorey69 storey = new <CreateProperties>c__AnonStorey69 {
                memberSerialization = memberSerialization,
                <>f__this = this
            };
            if (<>f__am$cache1 == null)
            {
                <>f__am$cache1 = x => !x.FieldType.Is(typeof(UnityEngine.Object)) && !x.FieldType.Is(typeof(Delegate));
            }
            if (<>f__am$cache2 == null)
            {
                <>f__am$cache2 = x => x.IsPublic || (x.GetCustomAttributes(typeof(SerializeField), true).Length > 0);
            }
            List<JsonProperty> list = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where<FieldInfo>(<>f__am$cache1).Where<FieldInfo>(<>f__am$cache2).Select<FieldInfo, JsonProperty>(new System.Func<FieldInfo, JsonProperty>(storey.<>m__F9)).ToList<JsonProperty>();
            if (<>f__am$cache3 == null)
            {
                <>f__am$cache3 = delegate (JsonProperty p) {
                    p.Writable = true;
                    p.Readable = true;
                };
            }
            list.ForEach(<>f__am$cache3);
            return list;
        }

        protected override List<MemberInfo> GetSerializableMembers(System.Type objectType)
        {
            if (<>f__am$cache0 == null)
            {
                <>f__am$cache0 = x => x.MemberType == MemberTypes.Field;
            }
            return base.GetSerializableMembers(objectType).Where<MemberInfo>(<>f__am$cache0).ToList<MemberInfo>();
        }

        [CompilerGenerated]
        private sealed class <CreateProperties>c__AnonStorey69
        {
            internal GameJsonUtility.ContractResolver <>f__this;
            internal MemberSerialization memberSerialization;

            internal JsonProperty <>m__F9(FieldInfo f)
            {
                return this.<>f__this.CreateProperty(f, this.memberSerialization);
            }
        }
    }
}

